// pages/scenic/scenic.js
Page({
  data: {
    keyword: '', // 搜索关键词
    scenicAreas: [], // 景区列表
    loading: false, // 加载状态
    currentTab: 'all', // 当前选中的标签
    statusBarHeight: 0 // 状态栏高度
  },

  onLoad: function (options) {
    // 获取状态栏高度
    const systemInfo = wx.getSystemInfoSync();
    this.setData({
      statusBarHeight: systemInfo.statusBarHeight
    });
    
    this.loadScenicAreas();
  },
  
  // 处理景区数据，确保所有字段都有值
  processData: function(data) {
    return data.map(item => {
      return {
        id: item.id || 0,
        name: item.name || '未命名景区',
        address: item.address || '暂无地址信息',
        ticketPrice: item.ticketPrice || 0,
        openTime: item.openTime || '全天开放',
        imageUrl: item.imageUrl || '/img/logo.png',
        description: item.description || '暂无描述'
      };
    });
  },
  
  // 加载景区列表
  loadScenicAreas: function() {
    this.setData({ loading: true });
    
    // 根据当前标签设置不同的排序参数
    let params = {};
    switch (this.data.currentTab) {
      case 'popular':
        params.sort = 'popular';
        break;
      case 'nearby':
        params.sort = 'distance';
        break;
      case 'price':
        params.sort = 'price';
        break;
      default:
        params.sort = '';
    }
    
    wx.mRequest({
      url: '/api/scenic/list',
      method: 'GET',
      data: params,
      success: (res) => {
        // 添加演示数据，实际环境请移除
        let mockData = [];
        if (!res.data || res.data.length === 0) {
          mockData = [
            {
              id: 1,
              name: '黄山风景区成人票',
              address: '安徽省黄山市黄山区汤口镇',
              ticketPrice: 380,
              openTime: '7:00-17:00',
              imageUrl: '/img/logo.png'
            },
            {
              id: 2,
              name: '黄山温泉酒店标准间',
              address: '安徽省黄山市黄山区汤口镇温泉路18号',
              ticketPrice: 588,
              openTime: '全天开放',
              imageUrl: '/img/logo.png'
            }
          ];
        }
        
        const processedData = this.processData(res.data || mockData);
        
        this.setData({
          scenicAreas: processedData,
          loading: false
        });
      },
      fail: () => {
        // 添加演示数据，实际环境请移除
        const mockData = [
          {
            id: 1,
            name: '黄山风景区成人票',
            address: '安徽省黄山市黄山区汤口镇',
            ticketPrice: 380,
            openTime: '7:00-17:00',
            imageUrl: '/img/logo.png'
          },
          {
            id: 2,
            name: '黄山温泉酒店标准间',
            address: '安徽省黄山市黄山区汤口镇温泉路18号',
            ticketPrice: 588,
            openTime: '全天开放',
            imageUrl: '/img/logo.png'
          }
        ];
        
        const processedData = this.processData(mockData);
        
        this.setData({
          scenicAreas: processedData,
          loading: false
        });
        
        wx.showToast({
          title: '加载失败，使用模拟数据',
          icon: 'none'
        });
      }
    });
  },
  
  // 切换标签
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    if (tab !== this.data.currentTab) {
      this.setData({ currentTab: tab });
      this.loadScenicAreas();
    }
  },
  
  // 关键词输入事件
  onKeywordInput: function(e) {
    this.setData({
      keyword: e.detail.value
    });
  },
  
  // 搜索按钮点击事件
  onSearch: function() {
    if (!this.data.keyword.trim()) {
      this.loadScenicAreas();
      return;
    }
    
    this.setData({ loading: true });
    
    wx.mRequest({
      url: '/api/scenic/search',
      method: 'GET',
      data: {
        name: this.data.keyword
      },
      success: (res) => {
        const processedData = this.processData(res.data || []);
        
        this.setData({
          scenicAreas: processedData,
          loading: false
        });
      },
      fail: () => {
        this.setData({ loading: false });
        wx.showToast({
          title: '搜索失败，请重试',
          icon: 'none'
        });
      }
    });
  },
  
  // 跳转到景区详情页
  goToDetail: function(e) {
    const scenicId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/scenic/detail/detail?id=' + scenicId
    });
  },
  
  // 下拉刷新
  onPullDownRefresh: function() {
    this.loadScenicAreas();
    wx.stopPullDownRefresh();
  }
}) 